package com.zhj.test.nio.zerocopy;

import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @author zhj
 */
public class NewIOClient {
    public static void main(String[] args) throws Exception {
        InetSocketAddress address = new InetSocketAddress("127.0.0.1",7002);
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(address);
        String fileName = "";

        FileChannel channel = new FileInputStream(fileName).getChannel();
        long startTime = System.currentTimeMillis();
        // linux 下一个transferTo方法就可以完成传输
        // windows 下调用只能发8m,就需要分段传输文件，要注意传输位置 需要循环计算
        // 使用零拷贝
        long transferCount = channel.transferTo(0, channel.size(), socketChannel);
        System.out.println("发送总字节数： " + transferCount + " 耗时：" + (System.currentTimeMillis()-startTime));
        channel.close();
    }
}
